package com.heima.controller;

import com.heima.common.Result;
import com.heima.pojo.Partner;
import com.heima.pojo.dto.PartnerDto;
import com.heima.pojo.dto.PartnerPageDto;
import com.heima.pojo.vo.PartnerPageVo;
import com.heima.service.PartnerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/user-service/partner")
public class PartnerController {

    @Autowired
    PartnerService partnerService;

    /**
     * 新增合作商
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增合作商")
    public Result addPartner(@RequestBody PartnerDto dto){
        log.info("接收到的参数为:{}",dto);
        partnerService.addPartner(dto);
        return Result.success();
    }

    /**
     * 合作商分页查询
     */
    @GetMapping("/search")
    @ApiOperation("合作商分页查询")
    public PartnerPageVo pageQuery(PartnerPageDto dto){
        log.info("接收到的参数为:{}",dto);
        PartnerPageVo vo = partnerService.pageQuery(dto);
        log.info("返回给前端的数据为:{}",vo);
        return vo;
    }

    /**
     * 获取合作商详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取合作商详情")
    public Partner getInfo(@PathVariable Integer id){
        log.info("接到的参数:{}",id);
        return partnerService.getInfo(id);
    }

    /**
     * 修改合作商
     */
    @PutMapping("/{id}")
    @ApiOperation("修改合作商")
    public Boolean update(@PathVariable Integer id,@RequestBody @Validated PartnerDto dto){
        log.info("接收到的参数为:{},{}",id,dto);
        partnerService.update(id,dto);
        return true;
    }

    /**
     * 删除合作商
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除合作商")
    public Boolean delById(@PathVariable Integer id){
        log.info("id:{}",id);
        partnerService.delById(id);
        return true;
    }

    /**
     * 合作商数量
     */
    @ApiOperation("合作商数量查询")
    @GetMapping("/count")
    public Integer count(){
        log.info("查询合作商数量");
        Integer count = partnerService.count();
        return count;
    }

    /**
     * 重置合作商密码
     */
    @PutMapping("/resetPwd/{id}")
    public Boolean resetPassword(@PathVariable Integer id){
        log.info("id:{}",id);
        partnerService.resetPassword(id);
        return true;
    }
}
